fix(dxinput): fix property reading and button mapping data processing logic#175
Conversation
Reviewer's GuideRefactors XInput property and button mapping handling to correctly size, copy, and free C-allocated buffers, switching to length-aware APIs and GoBytes, and adding a dedicated C-side free helper to prevent leaks. Sequence diagram for updated GetProperty data and memory handlingsequenceDiagram
participant GoWrapper as Go_GetProperty
participant CLayer as C_get_prop
participant X11 as XInput_XIGetProperty
participant CFree as C_free_prop_data
GoWrapper->>CLayer: get_prop(id, prop, &nbytes)
activate CLayer
CLayer->>X11: XIGetProperty(length=0)
X11-->>CLayer: act_type, act_format, num_items, bytes_after, data
CLayer->>CLayer: check ret and act_type
CLayer->>CLayer: XFree(data) if not null
CLayer->>CLayer: if bytes_after == 0 return NULL
CLayer->>X11: XIGetProperty(length=(bytes_after+3)/4)
X11-->>CLayer: act_type, act_format, num_items, bytes_after, data
CLayer->>CLayer: compute nbytes = num_items * (act_format/8)
CLayer-->>GoWrapper: data pointer, nbytes
deactivate CLayer
GoWrapper->>GoWrapper: datas = C.GoBytes(data, nbytes)
GoWrapper->>CFree: free_prop_data(data)
CFree-->>GoWrapper:
GoWrapper-->>GoWrapper: return datas, int32(nbytes)
Sequence diagram for updated GetButtonMap data copyingsequenceDiagram
participant GoWrapper as Go_GetButtonMap
participant CLayer as C_get_button_map
GoWrapper->>CLayer: get_button_map(xid, devName, &cbtnNum)
CLayer-->>GoWrapper: cbtnMap pointer, cbtnNum
GoWrapper->>GoWrapper: btnMap = C.GoBytes(cbtnMap, cbtnNum)
GoWrapper->>CLayer: C.free(cbtnMap)
CLayer-->>GoWrapper:
GoWrapper-->>GoWrapper: return btnMap, nil
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
get_prop,nbytesis computed asnum_items * (act_format / 8)without validating thatact_formatis one of the expected 8/16/32 values or that the multiplication fits into anint; consider adding a format sanity check and range check to avoid silent truncation or zero-length results for unexpected formats. - The Go
GetPropertyAPI now returns a byte-length as the second value instead of an item count; if callers rely on an element count, it may be worth clarifying the unit in the function name or adding a small helper to convert to item counts based on the expected element size at call sites.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `get_prop`, `nbytes` is computed as `num_items * (act_format / 8)` without validating that `act_format` is one of the expected 8/16/32 values or that the multiplication fits into an `int`; consider adding a format sanity check and range check to avoid silent truncation or zero-length results for unexpected formats.
- The Go `GetProperty` API now returns a byte-length as the second value instead of an item count; if callers rely on an element count, it may be worth clarifying the unit in the function name or adding a small helper to convert to item counts based on the expected element size at call sites.
## Individual Comments
### Comment 1
<location> `dxinput/utils/property.c:103-105` </location>
<code_context>
- *nitems = (int)num_items;
- XCloseDisplay(disp);
+ // Calculate actual byte length based on format and num_items
+ // format is 8, 16, or 32 bits per item
+ *nbytes = (int)(num_items * (act_format / 8));
+ XCloseDisplay(disp);
</code_context>
<issue_to_address>
**issue (bug_risk):** Guard against integer truncation when converting num_items to int for nbytes.
`num_items` is an `unsigned long` but `*nbytes` is an `int`. On 32‑bit builds, `num_items * (act_format / 8)` can overflow or be truncated when cast to `int`, so Go may call `GoBytes` with a length smaller than the actual allocation. Please either:
- Validate `num_items <= INT_MAX / (act_format / 8)` and fail if it would overflow, or
- Use a wider type for the size (e.g., `long`/`size_t`) and update the Go side.
This avoids subtle memory corruption in large or pathological cases.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
5255c00 to
38aec02
Compare
logic - Replaced custom uchar array conversion function with C.GoBytes to simplify data copying - Modified get_prop function implementation to first obtain the attribute data size, then read the data completely - Corrected get_prop function parameters and comments; nbytes now indicates byte length rather than item count - Added free_prop_data function to release memory returned by get_prop, preventing memory leaks - Ensured proper release of C-allocated data after calling get_prop to retrieve properties in the Go layer - Removed unused maxBufferLen constant and the old ucharArrayToByte implementation PMS: BUG-315115 Influence: Input Device Management --- fix(dxinput): 修正属性读取和按钮映射数据处理逻辑 - 使用 C.GoBytes 替代自定义 uchar 数组转换函数简化数据拷贝 - 修改 get_prop 函数实现,先获取属性数据大小,再完整读取数据 - 修正 get_prop 函数参数和注释,nbytes 表示字节长度而非项目数 - 添加 free_prop_data 函数用于释放 get_prop 返回的内存,避免内存泄漏 - 在 Go 层调用 get_prop 获取属性后正确释放 C 分配的数据 - 删除无用的 maxBufferLen 常量和 ucharArrayToByte 旧实现 Influence: 输入设备管理功能
38aec02 to
e319d0c
Compare
deepin pr auto review这份代码修改主要涉及 X11 输入设备属性的获取与处理,包括 C 语言底层实现和 Go 语言封装层。以下是对代码的审查意见: 1. 语法逻辑优点:
改进建议:
2. 代码质量优点:
改进建议:
3. 代码性能优点:
改进建议:
4. 代码安全优点:
改进建议:
总结这次代码修改在安全性和健壮性方面有显著提升,特别是 C 层对内存管理和整数溢出的处理,以及 Go 层对数据校验的加强。性能方面也有小幅优化(使用 建议采纳上述关于 C 代码 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: electricface, fly602 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
to simplify data copying
attribute data size, then read the data completely
indicates byte length rather than item count
get_prop, preventing memory leaks
to retrieve properties in the Go layer
implementation
PMS: BUG-315115
Influence: Input Device Management
fix(dxinput): 修正属性读取和按钮映射数据处理逻辑
数组转换函数简化数据拷贝
函数实现,先获取属性数据大小,再完整读取数据
表示字节长度而非项目数
返回的内存,避免内存泄漏
分配的数据
Influence: 输入设备管理功能
Summary by Sourcery
Improve XInput property retrieval and button mapping data handling to use correct byte lengths and proper memory management between C and Go layers.
Bug Fixes:
Enhancements: